package com.appcenter.application.service;


import com.appcenter.application.dto.jobcategorys.JobCategoryDto;
import com.appcenter.application.input.jobCategory.*;
import com.appcenter.application.mapper.AppsMapper;
import com.appcenter.application.mapper.JobCategorysMapper;
import com.appcenter.application.mapper.JobsMapper;
import com.appcenter.application.model.JobCategorys;
import com.appcenter.application.output.jobCategory.*;
import com.appcenter.jz.app.CurrentUser;
import com.appcenter.jz.config.ErrorJsonResult;
import com.appcenter.jz.unit.ObjectUnit;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

import static org.apache.http.util.TextUtils.isBlank;

@Service
public class JobCategoryService {
    @Autowired
    JobCategorysMapper jobCategorysMapper;
    @Autowired
    AppsMapper appsMapper;
    @Autowired
    JobsMapper jobsMapper;
//    @Transactional
    /**
     *  创建人:Coke
     *  备注: 创建岗位分类
     *  时间: 2019/4/26 13:52 
     *  参数: 
     */
    public CreateJobCategoryOutput CreateJobCategory(CreateJobCategoryInput input) throws Exception {
        CreateJobCategoryOutput output = new CreateJobCategoryOutput();
        JobCategorys jobCategory=new JobCategorys();
        BeanUtils.copyProperties(input.getItem(),jobCategory);

        Example exampleJobCategory = new Example(JobCategorys.class);
        Example.Criteria criteria = exampleJobCategory.createCriteria();
        if (input.getItem().getAppCode() != null) {
            criteria.andEqualTo("appCode", input.getItem().getAppCode());
        }
        else {
            criteria.andIsNull("appCode");
        }
        criteria.andEqualTo("code", input.getItem().getCode());
        criteria.andEqualTo("deleted", false);
        int countJobCategory = jobCategorysMapper.selectCountByExample(exampleJobCategory);
        if (countJobCategory > 0) {
            throw new ErrorJsonResult("1000031", "分类编号已存在！");
        }

        String uuid = UUID.randomUUID().toString().replace("-", "").toLowerCase();
        jobCategory.setId(uuid);
        jobCategory.setCreateTime(new Date());
        jobCategory.setCreateUserId(CurrentUser.getUserId());
        jobCategory.setDeleted(false);
        if (isBlank(jobCategory.getParentId())) {
            jobCategory.setIdPath(uuid);
        }
        else{
            jobCategory.setIdPath(jobCategory.getIdPath()+"/"+uuid);
        }
        jobCategorysMapper.insertSelective(jobCategory);
        output.setItem(jobCategory);
        return output;

    }
//    @Transactional
    /**
     *  创建人:Coke
     *  备注: 修改岗位分类
     *  时间: 2019/4/26 13:52 
     *  参数: 
     */
    public EditJobCategoryOutput EditJobCategory(EditJobCategoryInput input) throws Exception {
        EditJobCategoryOutput output=new EditJobCategoryOutput();

        Example exampleJobCategory = new Example(JobCategorys.class);
        Example.Criteria criteriaJobCategory = exampleJobCategory.createCriteria();
        criteriaJobCategory.andEqualTo("code", input.getItem().getCode());

        if(input.getItem().getAppCode()==null){
            criteriaJobCategory.andIsNull("appCode");
        }else{
            criteriaJobCategory.andEqualTo("appCode", input.getItem().getAppCode());
        }
        criteriaJobCategory.andNotEqualTo("id", input.getItem().getId());
        criteriaJobCategory.andEqualTo("deleted", false);
        int countJobCategory = jobCategorysMapper.selectCountByExample(exampleJobCategory);
        if (countJobCategory > 0) {
            throw new ErrorJsonResult("1000031", "分类编号已存在！");
        }
        JobCategorys oldJobCategory=jobCategorysMapper.selectByPrimaryKey(input.getItem().getId());
        oldJobCategory.setCode(input.getItem().getCode());
        oldJobCategory.setName(input.getItem().getName());
        oldJobCategory.setRemarks(input.getItem().getRemarks());
        oldJobCategory.setSort(input.getItem().getSort());

        jobCategorysMapper.updateByPrimaryKey(oldJobCategory);

        output.setItem(oldJobCategory);
        return  output;
    }
//    @Transactional
    /**
     *  创建人:Coke
     *  备注: 获取岗位分类
     *  时间: 2019/4/26 13:52 
     *  参数: 
     */
    public List<JobCategorys> GetJobCategoryTreeList(String parentId, String AppCode){
        if(parentId!=null && parentId.length()>0) {

        }
        List<JobCategorys> jobCategorysList=jobCategorysMapper.GetJobCategoryTreeList(AppCode);
        return jobCategorysList;
    }
//    @Transactional
    /**
     *  创建人:Coke
     *  备注: 查询岗位分类
     *  时间: 2019/4/26 13:52 
     *  参数: 
     */
    public SearchJobCategoryOutput SearchJobCategory(SearchJobCategoryInput input) throws Exception {
        SearchJobCategoryOutput output = new SearchJobCategoryOutput();
        Map<String, JobCategoryDto> parentJobCategoryDtoDic = new HashMap<String, JobCategoryDto>();

        List<JobCategorys> jobCategorys=GetJobCategoryTreeList(null,input.getAppCode());
        List<JobCategoryDto> jobCategoryDtos = new ArrayList<JobCategoryDto>();
        if (jobCategorys != null && jobCategorys.size() > 0) {
            for (int i = 0; i < jobCategorys.size(); i++) {
                JobCategoryDto jobCategoryDto = new JobCategoryDto();
                BeanUtils.copyProperties(jobCategorys.get(i), jobCategoryDto);

                if (jobCategorys.get(i).getParentId() == null || !parentJobCategoryDtoDic.containsKey(jobCategorys.get(i).getParentId())) {
                    jobCategoryDtos.add(jobCategoryDto);
                } else {
                    JobCategoryDto parentJobCategoryDto = parentJobCategoryDtoDic.get(jobCategoryDto.getParentId());
                    if (parentJobCategoryDto.getChildren() == null) {
                        parentJobCategoryDto.setChildren(new ArrayList<JobCategoryDto>());
                    }
                    parentJobCategoryDto.getChildren().add(jobCategoryDto);
                }
                parentJobCategoryDtoDic.put(jobCategoryDto.getId(), jobCategoryDto);
            }
        }
        output.setJobCategorys(jobCategoryDtos);
        return output;
    }
//    @Transactional
    /**
     *  创建人:Coke
     *  备注: 修改岗位分类
     *  时间: 2019/4/26 13:52 
     *  参数: 
     */
    public EditJobCategoryOutput EditJobCategoryStatus(EditJobCategoryInput input){
        EditJobCategoryOutput output = new EditJobCategoryOutput();
        JobCategorys jobCategory=jobCategorysMapper.selectByPrimaryKey(input.getItem().getId());
        jobCategory.setValidStatus(input.getItem().getValidStatus());
        jobCategorysMapper.updateByPrimaryKey(jobCategory);
        output.setItem(jobCategory);
        return output;
    }

//    @Transactional
    /**
     *  创建人:Coke
     *  备注: 删除岗位分类
     *  时间: 2019/4/26 14:05
     *  参数: 
     */
    public DeleteJobCategoryOutput DeleteJobCategory(DeleteJobCategoryInput input){
        DeleteJobCategoryOutput output = new DeleteJobCategoryOutput();
        JobCategorys jobCategory=jobCategorysMapper.selectByPrimaryKey(input.getItem().getId());
        jobCategory.setDeleted(true);
        //删除子集分类
        deleteJobCategoryChildren(input.getItem().getId());
        jobCategorysMapper.updateByPrimaryKey(jobCategory);
        output.setItem(jobCategory);
        return output;
    }
    private void deleteJobCategoryChildren(String ParentId)
    {
        Example exampleJobCategory = new Example(JobCategorys.class);
        Example.Criteria criteriaJobCategory = exampleJobCategory.createCriteria();
        criteriaJobCategory.andEqualTo("parentId", ParentId);
        List<JobCategorys> jobCategorys=jobCategorysMapper.selectByExample(exampleJobCategory);
        for(JobCategorys jobCategory:jobCategorys)
        {
            jobCategory.setDeleted(true);
            jobCategorysMapper.updateByPrimaryKey(jobCategory);
            deleteJobCategoryChildren(jobCategory.getId());
        }
    }
}
